<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input size="default" class="w_200" v-model="listQuery.Name" placeholder="请输入部门名称" @keydown.enter="searchForm"> </el-input>
				<el-button size="default" @click="searchForm" type="primary" class="ml10"> 查询 </el-button>
				<el-button size="default" type="success" class="ml10" @click="addRule"> 新增部门 </el-button>
				<el-button size="default" type="info" class="ml10" @click="resetForm"> 重置 </el-button>
			</div>
			<tableTem ref="tableRef" v-bind:="state.tableData" @pageChange="onTablePageChange">
				<el-table-column align="center" type="index" label="序号" width="60" />
				<el-table-column align="center" prop="name" label="部门名称"></el-table-column>
				<el-table-column align="center" prop="description" label="备注"></el-table-column>
				<el-table-column align="center" prop="createTime" label="创建时间"></el-table-column>
				<el-table-column label="操作" align="center" width="150">
					<template #default="scope">
						<el-button size="small" type="primary" text @click="editOption(scope.row)">编辑</el-button>
						<el-button size="small" type="primary" text @click="getDetail(scope.row)">详情</el-button>
						<el-button size="small" type="primary" text @click="onRowDel(scope.row.id)">删除</el-button>
					</template>
				</el-table-column>
			</tableTem>
		</div>
		<!-- 弹窗 -->
		<el-dialog
			:title="state.dialogTitle"
			:close-on-click-modal="false"
			:show-close="false"
			:close-on-press-escape="false"
			v-model="state.dialogVisible"
			width="500px"
		>
			<dialogTem v-if="state.dialogType == 1" :edit-item="state.editItem" :isDisabled="state.isDisabled" @close="closeDialog" />
		</el-dialog>
	</div>
</template>

<script setup lang="ts" >
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { DepartmentGetPagedList, DepartmentDelete } from '/@/api/department/index';

// 引入组件
const dialogTem = defineAsyncComponent(() => import('./component/dialog.vue'));
const tableTem = defineAsyncComponent(() => import('/@/components/tables/index.vue'));

// 定义变量内容
const tableRef = ref();
const listQuery = reactive({
	Name: '',
});
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: true,
		param: {
			PageIndex: 1,
			PageSize: 10,
		},
	},
	editItem: {},
	isDisabled: false, // 详情
	dialogType: 0, // 弹窗类型
	dialogTitle: '', // 弹窗标题
	dialogVisible: false, // 弹窗状态
});
// 页面加载时
onMounted(() => {
	getTableData();
});
// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const params = {
			...state.tableData.param,
		};
		if (listQuery.Name) params.Name = listQuery.Name;
		let data = await DepartmentGetPagedList(params);
		state.tableData.data = data.data.items;
		state.tableData.total = data.data.totalCount;
	} finally {
		state.tableData.loading = false;
	}
};
// 弹窗打开
const openDialog = (title: any, type: any) => {
	state.dialogTitle = title;
	state.dialogType = type;
	state.dialogVisible = true;
};
// 新增
const addRule = () => {
	openDialog('新增', 1);
};
// 编辑
const editOption = (item: any) => {
	state.editItem = item;
	openDialog('编辑', 1);
};
// 详情
const getDetail = (item: any) => {
	state.editItem = item;
	state.isDisabled = true;
	openDialog('详情', 1);
};

// 关闭弹框
const closeDialog = () => {
	state.dialogTitle = '';
	state.dialogType = 0;
	state.dialogVisible = false;
	state.editItem = {};
	state.isDisabled = false;
	searchForm();
};

// 删除部门
const onRowDel = (id: any) => {
	ElMessageBox.confirm(`此操作将永久删除部门信息，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(async () => {
		await DepartmentDelete(id);
		ElMessage.success('删除成功');
		getTableData();
	});
};
// 分页改变
const onTablePageChange = (val: any) => {
	state.tableData.param.PageIndex = val.pageNum;
	state.tableData.param.PageSize = val.pageSize;
	getTableData();
};
// 搜索点击时表单回调
const searchForm = () => {
	state.tableData.param.PageIndex = 1;
	tableRef.value.pageReset();
};
// 重置
const resetForm = () => {
	listQuery.Name = '';
	searchForm();
};
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
